package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.utli.UserCountext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPermissionService permissionService;

    @ResponseBody
    @RequestMapping("/login")
    public JsonResult login(String username, String password, HttpSession session){
       try {
           Employee employee=employeeService.login(username,password);
           UserCountext.setCurrentUser(employee);
            if (!employee.getAdmin()){
                //若不是管理员,需要查询权限信息
                List<String> expressions= permissionService.queryByEmployeeId(employee.getId());
               UserCountext.setExpression(expressions);
            }
           return new JsonResult(true,"登陆成功");
       } catch(Exception e) {
           e.printStackTrace();
           return new JsonResult(false,"登陆失败");
       }
    }
}
